using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using Bookvar.ObjectModel;
using Bookvar.UI;

namespace BookvarWpf
{
    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Used in xaml binding")]
    internal class TopicTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate( object item, DependencyObject container )
        {
            TopicContentPresenter presenter = container as TopicContentPresenter;
            if ( presenter != null )
            {
                if ( presenter.Topic is MainTopic )
                {
                    return (DataTemplate) presenter.Owner.FindResource( "mainTopicTemplate" );
                }

                string templateSiffix = presenter.Topic.Depth == 1 ? "Level1" : string.Empty;

                if ( presenter.Owner.SelectedTopic == presenter.Topic )
                {
                    if ( presenter.X < presenter.Owner.MainTopicPresenter.X )
                    {
                        return (DataTemplate) presenter.Owner.FindResource( "leftSelectedTopicTemplate" + templateSiffix );
                    }
                    
                    return (DataTemplate) presenter.Owner.FindResource( "selectedTopicTemplate" + templateSiffix );
                }
                else
                {
                    if ( presenter.X < presenter.Owner.MainTopicPresenter.X )
                    {
                        return (DataTemplate) presenter.Owner.FindResource( "leftTopicTemplate" + templateSiffix );
                    }
                    
                    return (DataTemplate) presenter.Owner.FindResource( "topicTemplate" + templateSiffix );
                }
            }
            return null;
        }
    }
}